﻿var table = $(".dataTable");

$(function () {
    page.init();
});

var page = {
    height: function () {
        return $(window).innerHeight() * 80 / 100 + "px";
    },
    weight: function () {
        return $(window).innerWidth() * 80 / 100 + "px";
    },
    init: function () {
        $(".add").click(function () {
            page.open();
        });
        $(".edit").click(function () {
            var selectmodel = table.bootstrapTable('getSelections');
            if (selectmodel.length == 0) {
                page.tip("请先选中要修改的行~");
                return;
            }
            else if (selectmodel.length > 1) {
                page.tip("请选中一行~");
                return;
            }

            page.open(selectmodel[0].ID);
        });
        $(".delete").click(function () {
            var dataObj = table.bootstrapTable('getAllSelections');
            if (dataObj.length == 0) {
                page.tip("请先选中要删除的行~");
                return;
            }
            swal({
                title: "您确定要删除这条信息吗",
                text: "删除后将无法恢复，请谨慎操作！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "删除",
                cancelButtonText: "取消",
                closeOnConfirm: false
            },
                function () {
                    $.ajax({
                        type: "POST",
                        url: '/',
                        data: {
                            deldata: JSON.stringify(dataObj)
                        },
                        dataType: "json"
                    }).done(function (ret) {
                        if (ret.Restlt) {
                            swal(ret.Msg, "删除成功", "success");
                        } else {
                            swal(ret.Msg, "删除失败", "warning");
                        }
                        page.search();
                    }).fail(function () {
                        swal(ret.Msg, "出错了", "error");
                    });
                });
        });
        $(".refresh").click(function () {
            page.search();
        });
        $(".search").click(function () {
            page.search();
        });
        $(".reset").click(function () {
            document.getElementById("searchForm").reset();
        });

        page.data();
    },
    tip: function (tip) {
        swal({
            title: tip,
            confirmButtonText: "确定",
            type: "warning"
        });
    },
    open: function (id) {
        var url = '/Sys/SysUserEdit';
        if (id)
            url += '?id=' + id;

        layer.open({
            type: 2,
            shade: 0.7,
            shadeClose: true,
            title: '用户编辑',
            content: url,
            area: [page.weight(), page.height()],
        });
    },
    data: function () {
        table.bootstrapTable({
            url: '/Sys/SysUserList',
            method: 'post',
            showColumns: true,
            pagination: true,
            pageSize: 20,
            pageList: [10, 20, 50, 100],
            sidePagination: 'server',
            idField: 'Id',
            columns: [
                { checkbox: true },
                { field: 'Id', title: 'ID', visible: false },
                {
                    field: 'UserName', title: '用户名',
                    formatter: function (value, row, index) {
                        if (row != null)
                            return "<a href='javascript:void(0)'  onclick='page.open(" + row.Id + ")'>" + value + "</a>";
                    }
                },
                { field: 'Nick', title: '昵称' },
                { field: 'Email', title: '邮箱' },
                { field: 'Mobile', title: '手机' }
            ],
            queryParams: function (params) {
                return {
                    limit: params.limit,
                    offset: params.offset,
                    name: $("#userName").val()
                };
            },
            responseHandler: function (res) {
                if (res.Result) {
                    return {
                        rows: res.Data.rows,
                        total: res.Data.total
                    };
                } else {
                    swal("出错了..", res.Message, "error");

                    return {
                        rows: [],
                        total: 0
                    };
                }
            }
        });
    },
    search: function () {
        var opt = {
            url: '/Sys/SysUserList',
            silent: true,
            query: {
                name: $("#name").val()
            }
        };
        table.bootstrapTable('refresh', opt);
    }
};